// --------------------------------------------------------------------------------------------------------------------
// <summary>
//    Defines the Setup type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Resources;
using System.Xml.Serialization;
using BrewXML.Core.Data;
using BrewXML.Core.Services.NavigationServices;
using BrewXML.WindowsPhone.Data;
using BrewXML.WindowsPhone.Services.NavigationServices;
using Cirrious.CrossCore;

namespace BrewXML.WindowsPhone
{
    using Cirrious.MvvmCross.ViewModels;
    using Cirrious.MvvmCross.WindowsPhone.Platform;
    using Microsoft.Phone.Controls;

    /// <summary>
    ///    Defines the Setup type.
    /// </summary>
    public class Setup : MvxPhoneSetup
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Setup"/> class.
        /// </summary>
        /// <param name="rootFrame">The root frame.</param>
        public Setup(PhoneApplicationFrame rootFrame)
            : base(rootFrame)
        {
        }

        /// <summary>
        /// Creates the app.
        /// </summary>
        /// <returns>An instance of IMvxApplication.</returns>
        protected override IMvxApplication CreateApp()
        {
            return new MyApp();
        }

        public class MyApp : Core.App
        {
             #region Initialize Services

            protected override void InitializeServices()
            {
                //Create data when run app first time
                var storage = IsolatedStorageFile.GetUserStoreForApplication();
                if (storage.FileExists("Hops.xml"))
                {
                    
                }
                else
                {
                CreateData("Hops.xml","HOPS");
                CreateData("Miscs.xml", "MISCS");
                CreateData("Grains.xml", "GRAINS");
                CreateData("Styles.xml", "STYLES");
                CreateData("Recipes.xml", "RECIPES");
                }
                
               
                Mvx.RegisterSingleton<INavigationService>(new NavigationService());
            }
             #endregion

            #region Create Data

            public void CreateData(string filename, string rootname)
            {
                using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (storage.FileExists(filename))
                    {
                        //Load it
                    }
                    else
                    {
                        System.Windows.Resources.StreamResourceInfo strm = Application.GetResourceStream(new Uri("/BrewXML.WindowsPhone;component/Data/SAMPLE/" + filename, UriKind.Relative));
                        System.IO.StreamReader reader = new System.IO.StreamReader(strm.Stream);
                        string data = reader.ReadToEnd();
                        using (var file = storage.OpenFile(filename, System.IO.FileMode.Create))
                        {
                            using (var writer = new System.IO.StreamWriter(file))
                            {
                                writer.WriteLine(data);
                            }
                        }
                    }
                }
            }
            #endregion
        }

    }
}